<?php
//**************************************************************************
//
//            (C) Okulov Rostislav, 2011
//
//**************************************************************************

/*
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 *                                                                         *
 */

   include ('mysql_connect.php');
   include ('func_list.php');

   // Подключаемся к mysql серверу
   $link = mysql_connect($dbhost, $dbuser, $dbpassword);
   if (empty($link)) {
      echo 'Не удалось подключиться к MySQL'; 
      exit;
   }
   // Выбираем нашу базу данных
   mysql_select_db($dbname, $link);
   echo '<div style="width: 100% height: 100%; border: 1px solid rgb(145, 181, 255);">';
   echo '<h4>Статьи</h4>';
    if (isset($_GET['pid'])) {
       $query = "SELECT * FROM articles WHERE id=".intval($_GET['pid']);
       $result = mysql_query($query, $link);
       while ($articles = mysql_fetch_row($result)) {
          echo $articles[3].'<p><span style="font-size:70%;">'.$articles[1].'</span></p>';
          echo bbcode($articles[4]).'<p> Метки: ';
          $tags_out = explode(',',$articles[5]);
          for ($i=0;$i<count($tags_out);$i++) {
              echo '<a href="'.$_SERVER['PHP_SELF'].'?page=article&tag='.trim($tags_out[$i]).'">'.$tags_out[$i].'</a> ';
          }
          echo '</p>';

       }
    } elseif (isset($_GET['tag'])) {
          if (preg_match('/^[a-z0-9]{2,15}$/i',$_GET['tag'])) {
             echo '<strong>'.$_GET['tag'].'</strong><p>';
             $query = "SELECT id, post_dt, description FROM articles WHERE tags LIKE '%".$_GET['tag']."%'";
             $result = mysql_query($query, $link);
             while ($articles1 = mysql_fetch_row($result)) {               
                 echo '<a id="art_'.$articles1[0].'" href="'.$_SERVER['PHP_SELF'].'?page=article&pid='.$articles1[0].'">'.$articles1[2].'</a> '.$articles1[1].'<br />';
             }
             echo '</p>';
          }
    } else {
          $query = "SELECT id, post_dt, description FROM articles ORDER BY id DESC";
          $result = mysql_query($query, $link);
          while ($articles = mysql_fetch_row($result)) {
              echo '<a id="art_'.$articles[0].'" href="'.$_SERVER['PHP_SELF'].'?page=article&pid='.$articles[0].'">'.$articles[2].'</a> '.$articles[1].' <a style="color:blue" id="art_'.$articles[0].'" href="'.$_SERVER['PHP_SELF'].'?page=article&pid='.$articles[0].'#disqus_thread" data-disqus-identifier="'.$articles[0].'"></a><br />';
          }
    }
   mysql_close($link);

   echo '</div>';
?>
